<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');
require_once('../shared/confirmation.php');

// Load Storage Manager
require_once('../../storage/cloudinary.php');

class Index extends Layout {

    protected $objAlert, $dlgConfirmation;
    protected $objStorage;
    protected $dtrProjectGallery;
    protected $intId;
    protected $objProject;
    protected $pxyDelete;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project_gallery/menu/index.tpl.php';
        
        $this->objAlert = new Alert($this);
        $this->dlgConfirmation = new Confirmation($this, null, new QAjaxAction('dlgConfirmation_ConfirmAction'), null);
        $this->objStorage = new CloudinaryStorage();

        $this->intId = QApplication::QueryString('intId');
        $this->objProject = Project::Load($this->intId);

        $this->pxyDelete = new QControlProxy($this);
        $this->pxyDelete->AddAction(new QClickEvent(), new QAjaxAction('pxyDelete_Click'));
        $this->pxyDelete->AddAction(new QClickEvent(), new QTerminateAction());

        $this->dtrProjectGallery = new QDataRepeater($this);

        $this->dtrProjectGallery->Paginator = $this->dtrProjectGallery->PaginatorAlternate = new QPaginator($this);
        $this->dtrProjectGallery->ItemsPerPage = 10;
        
        $this->dtrProjectGallery->SetDataBinder('dtrProjectGallery_Bind');
        
        $this->dtrProjectGallery->Template = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project_gallery/template/dtrProjectGallery.tpl.php';
        
        $this->dtrProjectGallery->AddCssFile('../../../admin/content/css/project_gallery/index.css');
    }

    protected function dtrProjectGallery_Bind() {
        
        $this->dtrProjectGallery->TotalItemCount = ProjectGallery::CountByProjectId($this->intId);
        $this->dtrProjectGallery->DataSource = ProjectGallery::LoadArrayByProjectId($this->intId, QQ::Clause($this->dtrProjectGallery->LimitClause));
    }
    
    public function pxyDelete_Click($strFormId, $strControlId, $strParameter) {

        $objProjectGallery = ProjectGallery::Load($strParameter);

        $this->dlgConfirmation->strTitle = QApplication::Translate('Delete Image');
        $this->dlgConfirmation->strMessage = QApplication::Translate(sprintf('Do you want to delete "%s"?', $objProjectGallery->Description));
        $this->dlgConfirmation->btnConfirm->ActionParameter = $strParameter;
        $this->dlgConfirmation->Show();
    }

    public function dlgConfirmation_ConfirmAction($strFormId, $strControlId, $strParameter) {
        try {
            $objProjectGallery = ProjectGallery::Load($strParameter);

            $this->objStorage->Delete($objProjectGallery->Internal);

            $objProjectGallery->Delete();

            $this->dtrProjectGallery->Refresh();
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate(sprintf('Image "%s" Deleted Successful', $objProjectGallery->Description)));
            $this->dlgConfirmation->Hide();
        } catch (QCallerException $objExc) {
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
    }

}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project_gallery/index.tpl.php');
?>